import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:overlay_support/overlay_support.dart';
import 'package:provide/provide.dart';
import 'package:qgchat/common/class/upload/upload.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/states/states_model/user.dart';

class CoverVideo extends StatefulWidget {
  CoverVideo({Key key}) : super(key: key);

  @override
  _CoverVideoState createState() => _CoverVideoState();
}

class _CoverVideoState extends State<CoverVideo> {
  String videoUrl = '';

  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final video = Provide.value<UserState>(context).anchorInfo?.anchorVideo;
    if (video != null) {
      videoUrl = video;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          '封面视频',
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: px(36),
          ),
        ),
        actions: [
          IconButton(
            onPressed: () {
              if (videoUrl == "") {
                toast('请先上传视频');
                return;
              }
              UserApis.coverVideo(videoUrl).then((res) {
                Provide.value<UserState>(context).userInfo();
                toast('修改成功');
                Navigator.maybePop(context);
              });
            },
            icon: Text('保存'),
          ),
        ],
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.white,
        child: videoUrl == ''
            ? GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: choiceVideo,
                child: Container(
                  margin: EdgeInsets.all(px(30)),
                  width: px(116),
                  height: px(116),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(
                      width: px(3),
                      color: col('edeff2'),
                    ),
                  ),
                  child: Container(
                    width: px(56),
                    height: px(56),
                    child: Icon(
                      Icons.add,
                      size: px(200),
                      color: col('bfc2cc'),
                    ),
                  ),
                ),
              )
            : GestureDetector(
                onTap: choiceVideo,
                child: Container(
                  margin: EdgeInsets.all(px(30)),
                  height: px(400),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(
                      width: px(1),
                      color: col('edeff2'),
                    ),
                  ),
                  child: Image.network(
                    "$videoUrl?vframe/jpg/offset/0",
                    fit: BoxFit.cover,
                  ),
                ),
              ),
      ),
    );
  }

  choiceVideo() {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext ctx) {
        return CupertinoActionSheet(
          title: Text("选择视频"),
          message: Text("选择以下选项获取视频"),
          cancelButton: CupertinoActionSheetAction(
            onPressed: () => Navigator.pop(ctx),
            child: Text("取消"),
          ),
          actions: <Widget>[
            CupertinoActionSheetAction(
              onPressed: () async {
                try {
                  Navigator.pop(ctx);
                  final file = await ImagePicker.pickVideo(
                    source: ImageSource.camera,
                  );
                  final url = await Upload.qiniuUploadVideo(
                    context,
                    file,
                  );
                  videoUrl = url;
                  // thumbnail = "$url?vframe/jpg/offset/0";
                  setState(() {});
                } catch (e) {
                  if (e is String) toast(e);
                }
              },
              child: Text("相机"),
            ),
            CupertinoActionSheetAction(
              onPressed: () async {
                try {
                  Navigator.pop(ctx);
                  final file = await ImagePicker.pickVideo(
                    source: ImageSource.gallery,
                  );
                  final url = await Upload.qiniuUploadVideo(
                    context,
                    file,
                  );
                  videoUrl = url;
                  setState(() {});
                } catch (e) {
                  if (e is String) toast(e);
                }
              },
              child: Text("系统相册"),
            ),
          ],
        );
      },
    );
  }
}
